import Cell
import AStar
from random import *
import coords

class Map:
    def __init__(self):
        
        self.Miners = []
        self.BattleShips = []
        self.Pirates = []
        self.mineralsMined = 0
        
        i=0
        self.mapw = 32
        self.maph = 32
        self.mapSize = self.mapw*self.maph
                
        self.mapCells = []
        self.map = []
        
        gridCoords = [0,0]        
        screenCoords = [0,0]
        for i in range(self.mapSize):
            gridCoords[0] = i%self.mapw
            gridCoords[1] = i//self.mapw
            print 'gridCoords: ' + str(gridCoords)
            screenCoords = coords.gridToScreen(gridCoords)
            #print 'screenCoords: ' + str(screenCoords)
            self.mapCells.append(Cell.Cell(i, self.mapw))
            i+=1
        
        i=0
        for i in range(self.mapSize):
            #self.map.append(self.mapCells[i].security)
            self.map.append(0)
            i+=1
    
    def addMiner(self, miner):
        self.Miners.append(miner)
    
    def addPirate(self, pirate):
        self.Pirates.append(pirate)
        
    def addBattleShip(self, battleship):
        self.BattleShips.append(battleship)
        
    def addResources(self, xPos, yPos, resourceDensity):
        self.mapCells[(yPos*(self.mapw))+(xPos)].addResources(resourceDensity)
    
    def addObstacle(self, xPos, yPos):
        print str((yPos*(self.mapw))+(xPos)-1)
        self.mapCells[(yPos*(self.mapw))+(xPos)].beObstacle()
        self.map[(yPos*(self.mapw))+(xPos)] = "X"

    def getCellResources(self, myLocation):
        return self.mapCells[(myLocation[1]*(self.mapw))+(myLocation[0])].getResources()
    
    def mineResources(self, myLocation):
        self.mapCells[(myLocation[1]*(self.mapw))+(myLocation[0])].hasChanged = True
        return self.mapCells[(myLocation[1]*(self.mapw))+(myLocation[0])].mineResources()
    
    def depositMinerals(self, minerals):
        self.mineralsMined += minerals
        print "Resources: "+str(self.mineralsMined)
    
    def updateInfluenceMap(self):
        for i in range(1024):
            self.map[i] = 0
        #print "Printing map"
        #for Miner in self.Miners:
        #    self.map[(Miner.gridCoords[1]*self.mapw)+Miner.gridCoords[0]]
        """
        for Miner in self.Miners:
            print str(Miner.ship.myGridCoords[0])+","+str(Miner.ship.myGridCoords[1])
            self.map[(Miner.ship.myGridCoords[1]*self.mapw)+Miner.ship.myGridCoords[0]] += 6
            if Miner.ship.myGridCoords[0] > 0:
                self.map[(Miner.ship.myGridCoords[1]*self.mapw)+Miner.ship.myGridCoords[0]-1] += 3
                if Miner.ship.myGridCoords[1] > 0:
                    self.map[((Miner.ship.myGridCoords[1]-1)*self.mapw)+Miner.ship.myGridCoords[0]] += 3
                    self.map[((Miner.ship.myGridCoords[1]-1)*self.mapw)+Miner.ship.myGridCoords[0]-1] += 3
                if Miner.ship.myGridCoords[1] < 31:
                    self.map[((Miner.ship.myGridCoords[1]+1)*self.mapw)+Miner.ship.myGridCoords[0]-1] += 3
            if Miner.ship.myGridCoords[0] < 31:
                self.map[(Miner.ship.myGridCoords[1]*self.mapw)+Miner.ship.myGridCoords[0]+1] += 3
                if Miner.ship.myGridCoords[1] < 31:
                    self.map[((Miner.ship.myGridCoords[1]+1)*self.mapw)+Miner.ship.myGridCoords[0]] += 3
                    self.map[((Miner.ship.myGridCoords[1]+1)*self.mapw)+Miner.ship.myGridCoords[0]+1] += 3
                if Miner.ship.myGridCoords[1] > 0:
                    self.map[((Miner.ship.myGridCoords[1]-1)*self.mapw)+Miner.ship.myGridCoords[0]+1] += 3
        #for Pirate in self.Pirates:
        #    self.map[(Pirate.gridCoords[1]*self.mapw)+Pirate.gridCoords[0]] -= 5"""
        for i in range(1024):
            if self.mapCells[i].isObstacle() == 1:
                self.map[i] = "X"
            elif self.mapCells[i].getResources() > 20:
                self.map[i] = 3
            elif self.mapCells[i].getResources() > 10:
                self.map[i] = 2
            elif self.mapCells[i].getResources() > 0:
                self.map[i] = 1
            else:
                self.map[i] = 0

    def initialiseCells(self):
        for cell in self.mapCells:
            cell.initialiseSprite()
    
    def getCellSprites(self):
        return self.mapCells

    def printMap(self):
        row = ""
        j=0
        for i in range(1024):
            row += str(self.map[i-1])
            j+=1
            if j == 32:
                print row
                row = ""
                j=0
            
    
    def printAllCellCoords(self):
        for cell in self.mapCells:
            print str(cell.getGridCoords())
        
    def findPath(self, myLocation, desiredLocation):
        astar = AStar.AStar(AStar.SQ_MapHandler(self.map,self.mapw,self.maph))
        start = AStar.SQ_Location(myLocation[0],myLocation[1])
        end = AStar.SQ_Location(desiredLocation[0],desiredLocation[1])
        
        return astar.findPath(start,end) ##Path object .nodes[] .totalCost
    
    def findResources(self, myLocation, myLastLocation):
    #def findResources(self, myLocation):
        #print "myLocation"+str(myLocation)
        currentCell = self.mapCells[(myLocation[1]*(self.mapw))+(myLocation[0])]
        neighbours = self.getNeighbours(myLocation)
        #print "Number of neighbours: "+str(len(neighbours))
        for cell in neighbours:
            #print "Neighbour: "+str(cell.getGridCoords())
            #print str(cell.getResources())
            if cell.getResources() >= currentCell.getResources():
                 if cell.getGridCoords() != myLastLocation:
                     currentCell = cell
        #print "New Cell location: "+str(currentCell.getGridCoords())
        if currentCell.getResources() == 0:
            return self.mapCells[(myLocation[1]*(self.mapw))+(myLocation[0])].getGridCoords()
        return currentCell.getGridCoords()
    
    def newRandomCell(self, myLocation):
        #print "Getting a random cell"
        neighbours = self.getNeighbours(myLocation)
        #for cell in neighbours:
            #print "Neighbour: "+str(cell.getGridCoords())
        newCell = None
        while newCell == None:
            i = int(uniform(0,(len(neighbours)-1))//1)
            newCell = neighbours[i]
        return newCell.getGridCoords()
            
    def getNeighbours(self, myLocation):
        neighbours = []
        #print "Location passed to getNeighbours: "+str(myLocation)
        cell = self.getCell([myLocation[0],myLocation[1]+1])
        if cell: 
            neighbours.append(cell)
        cell = self.getCell([myLocation[0],myLocation[1]-1])
        if cell: 
            neighbours.append(cell)
        cell = self.getCell([myLocation[0]+1,myLocation[1]+1])
        if cell: 
            neighbours.append(cell)
        cell = self.getCell([myLocation[0]+1,myLocation[1]-1])
        if cell: 
            neighbours.append(cell)
        cell = self.getCell([myLocation[0]-1,myLocation[1]+1])
        if cell: 
            neighbours.append(cell)
        cell = self.getCell([myLocation[0]-1,myLocation[1]-1])
        if cell: 
            neighbours.append(cell)
        cell = self.getCell([myLocation[0]+1,myLocation[1]])
        if cell: 
            neighbours.append(cell)
        cell = self.getCell([myLocation[0]-1,myLocation[1]])
        if cell: 
            neighbours.append(cell)
        
        return neighbours
        

    def getCell(self, location):
        #print "location of neighbour to get: "+str(location)
        if location[0]<0 or location[0]>=self.mapw or location[1]<0 or location[1]>=self.maph:
            #print "returning none"
            return None
        #print "returning a cell"
        if self.mapCells[(location[1]*(self.mapw))+(location[0])].isObstacle() == 1:
            return None
        return self.mapCells[(location[1]*(self.mapw))+(location[0])]